package com.yls.except.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class GetHeadImg {
	public static String getheadimg(HttpServletRequest req, HttpSession session,String uid) {
		String src = ImgProperties.getimg("headimg");
		String defaultsrc = ImgProperties.getimg("defaultimg");
		String path = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+"/";
		String basePath = session.getServletContext().getRealPath("/"+src);
		String defaultpath = session.getServletContext().getRealPath("/"+defaultsrc);
		List<Object> list = ReadFile.getFile(basePath);
		String headurl = "";
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
			String [] imgurl = (list.get(i)+"").split("_");
			for (int j = 0; j < imgurl.length; j++) {
				if(imgurl[0].equals(uid) && imgurl[2].equals("middle.jpg")) {
					System.out.println(list.get(i)+"我是最终的图片路径");
					headurl = src + list.get(i)+"";
					break;
				}
			}
		}
		if(headurl == null || "".equals(headurl)) {
			List<Object> defaultlist = ReadFile.getFile(defaultpath);
			if(defaultlist.size() > 0) {
				path += defaultsrc + defaultlist.get(0);
			}
		} else {
			path += headurl;
		}
		return path;
	}
}
